package com.flash.ReportManagement.Service.impl;

import com.flash.ReportManagement.Service.CashPositionStatementService;
import com.flash.ReportManagement.entity.CashPositionStatement;
import com.flash.ReportManagement.mapper.CashPositionStatementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CashPositionStatementServiceImpl implements CashPositionStatementService {

    @Autowired
    CashPositionStatementMapper cashPositionStatementMapper;
    @Override
    public Map<String,Object> selectCashPositionStatement(String dateAccrualStock) {
        //创建一个结果集Map用于存放两个结果变量
        Map<String,Object> resultMap=new HashMap<>();
        if (dateAccrualStock==null){
            dateAccrualStock="2023-03-22";
        }
        Map<String,Object> map=new HashMap<>();
        map.put("dateAccrualStock",dateAccrualStock);
        //调用Mapper执行查询
        List<CashPositionStatement> cashPositionStatementList = cashPositionStatementMapper.selectCashPositionStatement(map);
        double qingsuankuan=0;
        double shengou=0;
        double cunkuan=0;
        double zonge=0;
        for (CashPositionStatement cashPositionStatement:cashPositionStatementList) {
            //判断账户名称为“申购赎回”
            if(cashPositionStatement.getAccountName().equals("申购赎回")){
                //算出申购金额
                shengou+=cashPositionStatement.getMoney();
                //判断账户名称为“清算款”
            }else if(cashPositionStatement.getAccountName().equals("清算款")){
                //清算款金额
                qingsuankuan+=cashPositionStatement.getMoney();
                //最后数据都为存款金额
            }else {
                //算出存款金额
                cunkuan+=cashPositionStatement.getMoney();
            }
        }
        //算出总额
        zonge=cunkuan+qingsuankuan+shengou;
        //创建四个构造方法账户名为"清算款合计"\"申购赎回合计"\"银行存款合计"\"可用寸头金额"
        CashPositionStatement cashPositionStatement1=new CashPositionStatement("清算款合计",qingsuankuan);
        CashPositionStatement cashPositionStatement2=new CashPositionStatement("申购赎回合计",shengou);
        CashPositionStatement cashPositionStatement3=new CashPositionStatement("银行存款合计",cunkuan);
        CashPositionStatement cashPositionStatement4=new CashPositionStatement("可用寸头金额",zonge);
        //将构造方法添加到集合中
        cashPositionStatementList.add(cashPositionStatement1);
        cashPositionStatementList.add(cashPositionStatement2);
        cashPositionStatementList.add(cashPositionStatement3);
        cashPositionStatementList.add(cashPositionStatement4);
        resultMap.put("cashPositionStatementList",cashPositionStatementList);
        return resultMap;
    }
}
